-- collect_energy2
-- created by dengc
-- 每回合收集能量，收集速度受生命值影响

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 每回合触发
    trigger = function(who, prop, arg)
        local attrib = "energy";
        local curValue = who.dbase:query(attrib, 0);
        local dbase = MonsterM.query(who.dbase:query("class_id"), "dbase");

        local maxValue = dbase["max_energy"] or 100;

        -- 能量未满，收集能量，否则发动攻击造成伤害
        if curValue < maxValue then
            local addNum = FormulaM.invoke("CALC_ORC_ENERGY_ADD_VALUE", who);
            addNum = math.min(addNum, maxValue - curValue);
            who.dbase:set(attrib, curValue + addNum);

            -- 恢复能量的速度增加了，记录一下
            local needEffect = false;
            local lastAdd = who.dbase:query("last_recover_energy") or 0;
            if lastAdd ~= 0 and lastAdd < addNum and addNum ~= 6 then
                needEffect = true;
            end

            -- 抛出事件
            EventMgr.fire(event.COLLECT_ENERGY, {
                ["amount"] = addNum, 
                ["pos"] = who:getPos(), 
                ["attrib"] = attrib, 
                ["need_effect"] = needEffect, 
            });

            -- 记录上一次恢复的能量
            who.dbase:set("last_recover_energy", addNum);
            
        else
            -- 触发属性
            local skillProp = PropM.combine(who, "apply_skill", 1);
            PropM.trigger(who, skillProp[1], skillProp[2]);
        end

        return true;
    end,

    record = function(target, prop, path)
        return false;
    end
};